package com.scy.hospitalmanager.controller;

import com.google.gson.Gson;
import com.scy.hospitalmanager.dao.nursedao.NurseLoginDao;
import com.scy.hospitalmanager.pojo.Nurse;
import com.scy.hospitalmanager.pojo.NurseLogin;
import com.scy.hospitalmanager.service.impl.NurseLoginServiceImpl;
import com.scy.hospitalmanager.until.MD5;
import com.scy.hospitalmanager.until.ResponseMap;
import com.scy.hospitalmanager.until.Result;
import com.scy.hospitalmanager.until.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 护士登陆
 * @ClassName: NurseLoginController
 * @Author: KuonnjiArisu
 * @Date: 2021/8/17 9:28
 * @Version: 1.0
 */
@Controller
@RequestMapping("/nurse")
public class NurseLoginController {
    @Autowired
    NurseLoginServiceImpl nurseLoginService;
    @Autowired
    NurseLoginDao nurseLoginDao;
    Long createTime = System.currentTimeMillis();
    Long invalidTime = System.currentTimeMillis() + 10 * 60 * 1000;

    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> nurseLogin(@RequestBody NurseLogin nurseLogin) {
        nurseLogin.setNlPassword(MD5.md5(nurseLogin.getNlPassword()));
        Map<String, Object> sessionUser = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        NurseLogin nurseLogin1 = nurseLoginService.loginByAccountAndByPassword(nurseLogin);
        System.out.println(nurseLogin1);
        if (nurseLogin1 != null) {
            sessionUser.put("nurseLogin", nurseLogin1);
            String token = TokenUtil.createToken(sessionUser, createTime, invalidTime);
            map.put("token", token);
            map.put("nurseLogin", nurseLogin1);
            return map;
        } else {
            return null;
        }

    }
    @PostMapping("/getAccount")
    @ResponseBody
    public ResponseMap getAccount(@RequestBody NurseLogin nurseLogin){
        return ResponseMap.OK(nurseLoginDao.getLoginByAccount(nurseLogin));
    }
    @PostMapping("/register")
    @ResponseBody
    public ResponseMap register(@RequestBody NurseLogin nurseLogin){
        if (nurseLoginService.insertLogin(nurseLogin)==1){
            return ResponseMap.OK("注册成功");
        }else {
            return ResponseMap.NO("注册失败");
        }

    }
    @Autowired
    private Gson gson;
    @PostMapping("/updateNurseLoginMsg")
    @ResponseBody
    public  ResponseMap updateNurse(@RequestBody NurseLogin nurseLogin,@RequestHeader("token") String token){
        if(nurseLogin.getNlPassword()!=""){
            nurseLogin.setNlPassword(MD5.md5(nurseLogin.getNlPassword()));
        }
        Map<String,Object>nurseMap= TokenUtil.parsePayloadData(token);
        String str = gson.toJson(nurseMap.get("nurseLogin"));
        String str2 = gson.toJson(nurseMap.get("currentTime"));
        NurseLogin nurseLogin1 = gson.fromJson(str,NurseLogin.class);
        nurseLogin.setNlNurseId(((nurseLogin1.getNlNurseId())));

        System.out.println(nurseLogin);
        return ResponseMap.OK(nurseLoginService.updateNurseLoginById(nurseLogin));

    }


}
